package com.itfenghuang.day03.service.imp;

import com.itfenghuang.bean.Student;
import com.itfenghuang.day03.mapper.StudentMapper;
import com.itfenghuang.day03.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 业务逻辑层实现类
 */
public class StudentServiceImp implements StudentService {

    @Override
    public List<Student> selectAllStus()  {
        InputStream is=null;
        List<Student> stus=null;
        SqlSession sqlSession=null;
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3.通过SqlSession工厂对象获取SqlSession对象
            sqlSession = factory.openSession();
            //4.获取StudentMapper接口的实现类对象 <T> T getMapper(Class<T> var1);
            //业务逻辑层实现数据访问层接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.执行方法
            stus = mapper.selectAllStus();
            //6.处理结果
            for (Student stu : stus) {
                System.out.println(stu);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stus;
    }

    @Override
    public int addStu(Student stu) {
        InputStream is =null;
        SqlSession sqlSession = null;
        int i = 0;

        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            i = mapper.addStu(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return i;
    }

    @Override
    public List<Student> selectStusByCondition(Student stu) {
        InputStream is=null;
        SqlSession sqlSession=null;
        List<Student> list =null;
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3.获取SqlSession对象
            sqlSession = factory.openSession();
            //4.获取接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.执行方法
            list = mapper.selectStusByCondition(stu);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.处理结果
        return list;
    }


}
